Skip to content

feat: join and fund an agent wallet in one transaction - #1409

Merged
MicBun merged 1 commit into
mainfrom
feat/maa-join-and-fund
Jul 22, 2026
Merged

feat: join and fund an agent wallet in one transaction#1409
MicBun merged 1 commit into
mainfrom
feat/maa-join-and-fund

Conversation

@MicBun

@MicBun MicBun commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Adds maa_join_and_fund($rule_id, $bridge, $amount), so a funder joins a rule and funds the derived agent wallet in a single transaction instead of a join followed by a separate transfer. Either both legs commit or neither does, so activation can no longer strand a joined-but-unfunded wallet.

The node half of the atomic-activation ask:

resolves: https://github.com/trufnetwork/truf-network/issues/1399

A kwil transaction runs exactly one action, so the SDKs cannot compose join + transfer atomically on the client — the two legs can only share a transaction inside one on-chain action. The joinAndFundAgentAddress SDK wrapper that exposes this (and closes #1399) is the follow-up.

What is here

  • internal/migrations/054-maa-join-fund.sql — the composite action. Leg 1 delegates to maa_join (all its guards apply: unknown rule, creator-self-join, duplicate join). Leg 2 debits the funder and credits the fresh wallet through 049's existing bridge dispatcher. The whole body runs in the route's nested transaction, so any failure rolls back both legs.
  • tests/streams/maa/join_and_fund_test.go — happy path, input validation, atomicity (insufficient balance and bad bridge each roll the join back), and duplicate-join plus a second funder deriving a distinct wallet.

Design notes

  • Bridge-agnostic, no .prod.sql twin. The action names no bridge; it only calls 049's dispatcher, whose mainnet twin already remaps the bridge branches. A scan confirms no bridge alias appears in the file.
  • Fund-free, like the other MAA setup actions — no wrapper fee, no transaction event. Top-ups after activation keep using the per-bridge transfer actions.
  • Never allow-list this action in a rule (header carries the warning): unlike the fund-free maa_join it moves funds, so through the maa_exec route an owner could self-strand into a keyless nested wallet. A restricted agent is blocked by the transfer guard, so no untrusted party can strand or steal; this is a wallet-signed activation call, not an agent action.

Worth a look

  • The amount guard is two separate ifs, not IS NULL OR <= 0. The engine's OR returns NULL for TRUE OR NULL rather than TRUE, so the combined form skips the guard on a NULL amount and lets it fall through to the transfer. The same dead pattern exists in 049's withdraw guard (atomic-safe, but its IS NULL half never fires) — left untouched here, flagged for a follow-up.

Testing

Full tests/streams/maa package green, including the four new tests. New migration parses (kwil-cli utils parse).

Summary by CodeRabbit

  • New Features

    • Added an atomic join-and-fund action for modular agent addresses.
    • Validates funding amounts and prevents invalid or unauthorized transfers.
    • Returns the derived agent address after successful registration and funding.
    • Rolls back the join if funding fails, preventing partial state changes.
  • Tests

    • Added coverage for successful funding, validation errors, rollback behavior, repeat joins, and multiple funders.

@MicBun MicBun self-assigned this Jul 22, 2026
@holdex

holdex Bot commented Jul 22, 2026

Copy link
Copy Markdown

Time Submission Status

Member # Time Running Total Status Last Update
MicBun 4h ✅ Submitted Jul 22, 2026, 9:27 AM

Submit or update total time with:

@holdex pr submit-time 2h

Add time on top of previous submission with:

@holdex pr add-time 1h30m

See available commands to help comply with our Guidelines.

@coderabbitai

coderabbitai Bot commented Jul 22, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Adds the public maa_join_and_fund action, which atomically joins a rule and funds the derived MAA. Integration tests cover validation, balances, audit events, rollback, duplicate joins, and multiple funders.

Changes

MAA Join and Fund

Layer / File(s) Summary
Atomic join-and-fund action
internal/migrations/054-maa-join-fund.sql
Adds amount validation, invokes maa_join, dispatches funds through the selected bridge, and returns the derived MAA address.
Integration test setup and helpers
tests/streams/maa/join_and_fund_test.go
Registers the schema test cases and adds helpers for action invocation, MAA lookup, address funding, and golden-address decoding.
Behavior and transaction validation
tests/streams/maa/join_and_fund_test.go
Verifies successful funding, rejected inputs, rollback on fund-leg failures, duplicate activation, distinct MAAs, balances, and audit events.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

Suggested labels: type: feat

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant maa_join_and_fund
  participant maa_join
  participant maa_dispatch_transfer
  Caller->>maa_join_and_fund: rule_id, bridge, amount
  maa_join_and_fund->>maa_join: Join rule and derive MAA
  maa_join-->>maa_join_and_fund: maa_address
  maa_join_and_fund->>maa_dispatch_transfer: Dispatch amount to derived MAA
  maa_dispatch_transfer-->>Caller: Return maa_address
Loading
🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The PR implements MAA join-and-fund logic, not the wallet-address portfolio getters requested in issue #1399. Revise the PR to implement the linked wallet portfolio getters, or relink the issue if this change is intentional.
Out of Scope Changes check ⚠️ Warning All changed files add an unrelated MAA join-and-fund flow, which is outside the linked issue's read-only order-book scope. Move this work to a separate PR or replace it with the issue's address-parameterized portfolio getter implementation.
✅ Passed checks (3 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: atomically joining a rule and funding the derived agent wallet.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/maa-join-and-fund

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@MicBun MicBun added the type: feat New feature or request label Jul 22, 2026
@MicBun

MicBun commented Jul 22, 2026

Copy link
Copy Markdown
Contributor Author

@holdex pr submit-time 4h

@MicBun
MicBun merged commit 6ba0ff7 into main Jul 22, 2026
9 checks passed
@MicBun
MicBun deleted the feat/maa-join-and-fund branch July 22, 2026 09:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: feat New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant